package cn.g31010.sip.component
{
	import cn.g31010.sip.view.SelectCamWin;
	
	import flash.display.DisplayObject;
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	public class PublishVideo extends Video
	{
		private var nc:NetConnection;
		private var ns:NetStream;
		public var address:String;
		public var streamName:String
		private var cam:Camera;
		private var mic:Microphone;
		public var notify:Function;
		public var parentUI:DisplayObject;
		//public var autoConnect:Boolean=false;
		public function PublishVideo(streamName:String,address:String,notify:Function=null)
		{
			super();
			this.streamName=streamName;
			this.address=address;
			this.notify=notify;
		}
		
		public function connect(event:CloseEvent=null):void{
			if(isConnected()){
				close();
			}
			clear();
			if(event==null||event.detail == Alert.OK){
				NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF3;
				nc=new NetConnection();
				nc.client=new Client();
				nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);
				nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncerr);
				nc.connect(address);
				initCamera();
			}
			
		}
		private var closed:Boolean=false;
		public function close():void{
			cam=Camera.getCamera(null);
			cam=null;
			closed=true;
			if(ns!=null){
				ns.attachAudio(null);
				ns.attachCamera(null);
				ns.close();
			}
			if(nc!=null){
				nc.close();
			}
			clear();
		}
		
		public function pause():void{
			pauseVideo();
			pauseAudio();
		}
		
		public function resume():void{
			resumeVideo();
			resumeAudio();
		}
		
		public function pauseVideo():void{
			if(ns!=null&&isConnected())
			ns.attachCamera(null);
		}
		
		public function resumeVideo():void{
			if(ns!=null&&isConnected())
			ns.attachCamera(cam);
		}
		
		public function pauseAudio():void{
			if(ns!=null&&isConnected())
			ns.attachAudio(null);
		}
		
		public function resumeAudio():void{
			if(ns!=null&&isConnected())
			ns.attachAudio(mic);
		}
		
		public function publish():void{
			if(ns!=null&&isConnected())
			ns.publish (streamName,"live");
		}
		
		public function record():void{
			if(ns!=null&&isConnected())
			ns.publish(streamName,"record");
		}
		
		public function isConnected():Boolean{
			if(nc==null)
			return false;
			return nc.connected;
		}
		
		public function initCam(c:Camera):void{
			clear();
			this.cam=c;
			cam.setMode(160,120,25);
			cam.setQuality (0,85);
			cam.setLoopback(false);
			mic=Microphone.getMicrophone();
			attachCamera(cam);
			if(ns!=null){
				ns.attachAudio (mic);
				ns.attachCamera (cam);
			}
		}
		
		internal function initCamera(event:CloseEvent=null):void{
			if(event==null||event.detail == Alert.OK){
				if(Camera.names.length>1){
					var select:SelectCamWin=new SelectCamWin();
					select.cameras=Camera.names;
					select.publishVideo=this;
					PopUpManager.addPopUp(select,parentUI,false);
					PopUpManager.centerPopUp(select);
				}else{
					cam=Camera.getCamera();
					if(cam!=null){
						cam.setMode(160,120,25);
						cam.setQuality(0,85);
						cam.setLoopback(false);
						mic=Microphone.getMicrophone();
						attachCamera(cam);
					}else{
						Alert.show("没有找到摄像头，是否重新查找。","提示：",Alert.OK|Alert.NO,null,initCamera);
					}
				}
            	
            }
		}
		
		internal function asyncerr(e:AsyncErrorEvent):void{
            Alert.show(e.error.message);
        }
        internal function checkConnect (e:NetStatusEvent):void{
			var connected:Boolean=(e.info.code=="NetConnection.Connect.Success");
			closed=false;
			if (connected){
				ns = new NetStream(nc);
				ns.addEventListener(NetStatusEvent.NET_STATUS,nsstatus);
				ns.attachAudio (mic);
				ns.attachCamera (cam);
				if(notify==null){
					publish();
				}else{
					notify.call();
				}
			}else if(e.info.code=="NetConnection.Connect.Closed"){

			}else{
//				if(autoConnect){
//					connect();
//				}else{
//					
//				}
				//Alert.show(e.info.code+"\n连接失败，是否重新连接？","提示：",Alert.OK|Alert.NO,null,connect);
			}
		}
		
		private function nsstatus(e:NetStatusEvent):void{
			var info:Object=e.info;
			//Alert.show(info.code);
	if (info.code == "NetStream.Buffer.Full") {
              trace("播放状态： "+info.code);
              trace("缓冲区已满");
//              btn_pause.shape_play._visible = true;
//              btn_pause.shape_pause._visible = false;
       } else if (info.code == "NetStream.Buffer.Empty") {
              trace("播放状态： "+info.code);
              trace("数据缓冲区为空");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Failed") {
              trace("播放状态： "+info.code);
              trace("传输中有错误发生");
              //无需反馈
       } else if (info.code == "NetStream.Pause.Notify") {
              trace("播放状态： "+info.code);
              trace("播放暂停");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Play.Failed") {
              trace("播放状态： "+info.code);
              trace("播放出错");
              //严重错误
       } else if (info.code == "NetStream.Play.PublishNotify") {
              trace("播放状态： "+info.code);
              trace("发布开始，信息已经发送到所有订阅者");
              //无需反馈
       } else if (info.code == "NetStream.Play.Reset") {
              trace("播放状态： "+info.code);
              trace("播放列表重置");
              //无需反馈
       } else if (info.code == "NetStream.Play.Start") {
              trace("播放状态： "+info.code);
              trace("播放开始");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Play.Stop") {
              trace("播放状态： "+info.code);
              trace("播放停止");
              //无需反馈
       } else if (info.code == "NetStream.Play.StreamNotFound") {
              trace("播放状态： "+info.code);
              trace("播放的实时流或文件不存在");
              //严重错误
       } else if (info.code == "NetStream.Play.UnpublishNotify") {
              trace("播放状态： "+info.code);
              trace("发布停止，信息已经发送到所有订阅者");
              //无需反馈
       } else if (info.code == "NetStream.Publish.BadName") {
              trace("播放状态： "+info.code);
              trace("发布的数据流已经被发布");
              //无需反馈
       } else if (info.code == "NetStream.Publish.Idle") {
              trace("播放状态： "+info.code);
              trace("发布者闲置过长");
              //严重错误
       } else if (info.code == "NetStream.Publish.Start") {
              trace("播放状态： "+info.code);
              trace("发布开始");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Record.Failed") {
              trace("播放状态： "+info.code);
              trace("录制文件出错");
              //严重错误
       } else if (info.code == "NetStream.Record.NoAccess") {
              trace("播放状态： "+info.code);
              trace("没有足够的文件访问权限");
              //严重错误
       } else if (info.code == "NetStream.Record.Start") {
              trace("播放状态： "+info.code);
              trace("文件录制开始");
              //无需反馈
       } else if (info.code == "NetStream.Record.Stop") {
              trace("播放状态： "+info.code);
              trace("录制暂停");
              //无需反馈
       } else if (info.code == "NetStream.Seek.Failed") {
              trace("播放状态： "+info.code);
              trace("数据流定位出错");
              //严重错误
       } else if (info.code == "NetStream.Seek.Notify") {
              trace("播放状态： "+info.code);
              trace("数据搜索定位成功");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Unpause.Notify") {
              trace("播放状态： "+info.code);
              trace("恢复播放");
//              btn_pause.shape_play._visible = false;
//              btn_pause.shape_pause._visible = true;
       } else if (info.code == "NetStream.Unpublish.Success") {
              trace("播放状态： "+info.code);
              trace("停止发布");
              //无需反馈
       }
		}

	}
}